package com.atguigu.rabbitmq.springbootrabbitmq.controller;

import com.atguigu.rabbitmq.springbootrabbitmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/ttl")
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("/sendMsg/{message}")
    public void  sendMsg(@PathVariable String message){
        log.info("当前时间:{},发送一条信息给两个ttl队列:{}",new Date().toString(),message);
        rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列"+message);
    }
    @GetMapping("/sendMsg/{message}/{ttlTime}")
    public void  sendMsg(@PathVariable String message,@PathVariable String ttlTime){
        log.info("当前时间:{},发送一条延时{}ms信息给QCttl队列:{}",new Date().toString(),ttlTime,message);

        rabbitTemplate.convertAndSend("X","XC","消息来自ttl为40s的队列"+message,msg->{
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });
    }
    @GetMapping("/sendDelayMsg/{message}/{delayTime}")
    public void  sendDelayMsg(@PathVariable String message,@PathVariable Integer delayTime){
        log.info("当前时间:{},发送一条延时{}ms信息给QC延迟队列:{}",new Date().toString(),delayTime,message);

        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,DelayedQueueConfig.DELAYED_ROUTING_KEY,"消息来自ttl为40s的队列"+message, msg->{
            msg.getMessageProperties().setDelay(delayTime);
            return msg;
        });
    }

}
